package com.ruoyi.system.facade;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.system.domain.SysLogininfor;
import com.ruoyi.system.service.ISysLogininforService;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VisitorFacade {
    @Autowired
    private ISysLogininforService logininforService;

    public void record(){
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr();
        // 获取客户端操作系统
        String os = userAgent.getOperatingSystem().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();
        // 访问地点
        String address = AddressUtils.getRealAddressByIP(ip);
        // 封装对象
        SysLogininfor logininfor = new SysLogininfor();
        logininfor.setUserName("访客");
        logininfor.setIpaddr(ip);
        logininfor.setLoginLocation(address);
        logininfor.setStatus("2");
        logininfor.setBrowser(browser);
        logininfor.setOs(os);
        logininfor.setMsg("NULLPOINT");
        logininforService.insertLogininfor(logininfor);
    }
}
